package com.ss.android.common.app;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.view.View;

import com.bytedance.common.utility.collection.WeakList;
import com.ss.android.common.app.permission.PermissionsManager;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public abstract class AbsFragment extends Fragment implements IComponent, LifeCycleInvoker, UIScreenContext {
    private WeakList<LifeCycleMonitor> mMonitors = new WeakList();
    private Map mScreenLeaveContext = new HashMap();
    protected boolean mStatusActive;
    protected boolean mStatusDestroyed;
    protected boolean mStatusViewValid;

    public boolean isActive() {
        return this.mStatusActive;
    }

    public boolean isDestroyed() {
        return this.mStatusDestroyed;
    }

    public boolean isViewValid() {
        return this.mStatusViewValid;
    }

    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        this.mStatusActive = false;
        this.mStatusViewValid = false;
        this.mStatusDestroyed = false;
    }

    public void onDestroy() {
        super.onDestroy();
        this.mStatusViewValid = false;
        this.mStatusDestroyed = true;
        if (!this.mMonitors.weakHashMap.isEmpty()) {
            Iterator<LifeCycleMonitor> iterator = this.mMonitors.iterator();
            while (iterator.hasNext()) {
                LifeCycleMonitor monitor = iterator.next();
                if (monitor == null) {
                    continue;
                }
                monitor.onDestroy();
            }
            this.mMonitors.weakHashMap.clear();
        }
    }

    public void onDestroyView() {
        super.onDestroyView();
        this.mStatusViewValid = false;
    }

    public void onPause() {
        super.onPause();
        this.mStatusActive = false;
        if (!this.mMonitors.weakHashMap.isEmpty()) {
            Iterator<LifeCycleMonitor> iterator = this.mMonitors.iterator();
            while (iterator.hasNext()) {
                LifeCycleMonitor monitor = iterator.next();
                if (monitor == null) {
                    continue;
                }
                monitor.onPause();
            }
        }
    }

    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        PermissionsManager.getInstance().notifyPermissionsChange(this.getActivity(), permissions, grantResults, false);
    }

    public void onResume() {
        super.onResume();
        this.mStatusActive = true;
        if (!this.mMonitors.weakHashMap.isEmpty()) {
            Iterator<LifeCycleMonitor> iterator = this.mMonitors.iterator();
            while (iterator.hasNext()) {
                LifeCycleMonitor lifeCycleMonitor = iterator.next();
                if (lifeCycleMonitor == null) {
                    continue;
                }
                lifeCycleMonitor.onResume();
            }
        }
    }

    public void onSaveInstanceState(Bundle bundle) {
        if (bundle != null) {
            bundle.putBoolean("WORKAROUND_FOR_BUG_19917_KEY", true);
        }
        super.onSaveInstanceState(bundle);
    }

    public void onStop() {
        super.onStop();
        this.mStatusActive = false;
        if (!this.mMonitors.weakHashMap.isEmpty()) {
            Iterator<LifeCycleMonitor> iterator = this.mMonitors.iterator();
            while (iterator.hasNext()) {
                LifeCycleMonitor lifeCycleMonitor = iterator.next();
                if (lifeCycleMonitor == null) {
                    continue;
                }
                lifeCycleMonitor.onStop();
            }
        }
    }

    public void onViewCreated(View view, Bundle bundle) {
        super.onViewCreated(view, bundle);
        this.mStatusViewValid = true;
    }

    public void registerLifeCycleMonitor(LifeCycleMonitor lifeCycleMonitor) {
        this.mMonitors.add(lifeCycleMonitor);
    }

    public void setEnterContext(Map enterContext) {
        if (enterContext != null) {
            enterContext.isEmpty();
        }
    }

    public void setLeaveContext(Map leaveContext) {
        if (leaveContext != null && !leaveContext.isEmpty()) {
            Iterator<Map.Entry> iterator = leaveContext.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry entry = iterator.next();
                this.mScreenLeaveContext.put(entry.getKey(), entry.getValue());
            }
        }
    }

    public void setUserVisibleHint(boolean userVisibleHint) {
        super.setUserVisibleHint(userVisibleHint);
    }

    public void unregisterLifeCycleMonitor(LifeCycleMonitor lifeCycleMonitor) {
        this.mMonitors.remove(lifeCycleMonitor);
    }
}

